<?php

require_once DOC_ROOT . '/src/php/Smarty.php';
require_once DOC_ROOT . '/src/php/Model.php';

class PIndex extends PModel {

    public function indexAction() {
        $this->assign('file_list', PConfig::$fileList);
        $this->assign('data', array(
            'index' => PUtil::fillNumber(count(scandir(DOC_ROOT . PConfig::$fileList[1])) - 2),
        ));
        $this->display('index.tpl');
    }

    public function tplAction() {
        $this->display('api_tpl.tpl');
    }

    public function indexDraftAction() {
        $filename = $_GET['file_name'];
        $td = $_GET['path'];

        if (isset($filename) && isset($td)) {
            $rs = PDbconnect::query('select * from ' . PConfig::$tablecn[$td] . ' where p_filename in ("' . $filename . '","' . $filename . '_draft")');
            if (empty($rs)) {
//                $this->display('return.tpl');
                echo json_encode(array(
                    'ret' => -1,
                    'msg' => 'No Data!',
                ));
            } else {
                // 如果草稿和非草稿都存在，那么加载非草稿的
                $data = array(
                    'path' => $rs[0]['p_path'],
                    'file_name' => $filename,
                    'index' => $rs[0]['p_index'],
                    'title' => $rs[0]['p_title'],
                    'title_tip' => $rs[0]['p_titletip'],
                    'title_desc' => $rs[0]['p_titledesc'],
                    'arguments' => $rs[0]['p_arguments'],
                    'arguments_desc' => PUtil::unDisposeList($rs[0]['p_argumentsdesc']),
                    'attributes' => $rs[0]['p_attributes'],
                    'attributes_desc' => PUtil::unDisposeList($rs[0]['p_attributesdesc']),
                    'methods' => $rs[0]['p_methods'],
                    'methods_desc' => PUtil::unDisposeList($rs[0]['p_methodsdesc']),
                    'return' => $rs[0]['p_return'],
                    'return_desc' => $rs[0]['p_returndesc'],
                    'code' => $rs[0]['p_code'],
                );

                $this->assign('file_list', PConfig::$fileList);
                $this->assign('data', $data);
                $this->display('index.tpl');
            }
        } else {
            echo json_encode(array(
                'ret' => -1,
                'msg' => 'Lack parameter!',
            ));
        }
    }

    public function fetchAction($tpl, $value) {
        $this->assign('data', $value);
        return $this->fetch($tpl);
    }

    public function makenavAction($tpl, $value) {
        $this->assign('data', $value);
        $this->display($tpl);
    }

}